import moment from 'moment';
class EventCenter {
  constructor(id) {
    this.create(id);
  }
  // 表单id
  formId = null;
  // 实例
  instance = null;
  // 连接器
  connector = {};
  // 订阅器
  listeners = [];
  // 日志
  logList = [];

  create(id) {
    this.formId = id;
    this.instance = this;
  }
  setConnector = (connector) => {
    this.connector = connector;
  };
  addListener = (listener) => {
    this.listeners.push(listener);
  };
  removeLister = (listener) => {
    this.listeners = this.listeners.filter((item) => item != listener);
  };
  addLog = ({ name, info }) => {
    const event = { name, info, date: moment().valueOf() };
    this.logList.push(event);
  };
  showLog = () => {
    this.logList.forEach((item) => {
      console.log(item);
    });
  };
  notify = (params = {}) => {
    this.addLog({ name: params.topic, info: params });
    2;
    this.listeners.forEach((listener) => {
      listener(params);
    });
  };
}

export default EventCenter;
